{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 install torchei torchvision matplotlib\n",
    "!mkdir dataset\n",
    "!cd data\n",
    "!wget https://raw.githubusercontent.com/TorchEI/TorchEI/main/datasets/ilsvrc_valid8.pt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torchvision import models\n",
    "import torchei\n",
    "from matplotlib import pyplot as plt\n",
    "from IPython.display import display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = models.resnet18(pretrained=True)\n",
    "data = torch.load(\"data/ilsvrc_valid8.pt\")\n",
    "print(data.shape)\n",
    "plt.imshow(data[0][0].permute(1, 2, 0))\n",
    "plt.show()\n",
    "with open(\"data/imagenet_classes.txt\", \"r\") as f:\n",
    "    categories = [s.strip() for s in f.readlines()]\n",
    "print(f\"this is a {categories[torchei.get_result(model,data)[0].int().item()]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "to_cuda = torch.cuda.is_available()\n",
    "fault_model = torchei.fault_model(model, data, to_cuda=to_cuda)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fault_model.emat_attack(10, 1e-3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fault_model.sern_calc(output_class=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fault_model.outlierDR_protection()\n",
    "fault_model.emat_attack(10, 1e-3)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "152d055222e7a836abe9ae05b16a7c29ad5bbee3434128148231c161bd854273"
  },
  "kernelspec": {
   "display_name": "Python 3.9.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
